//
//  SCNMoreViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/2/6.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit
import Alertift

class SCNMoreViewController: SCNTableViewController {
    
    let dataArray = [[["img" : "icon_more_notice","title" : "消息推送"]],
                             [["img" : "icon_more_info","title" : "关于守财奴"],
                              ["img" : "icon_more_question","title" : "常见问题"],
                              ["img" : "icon_more_feedback","title" : "安全保障"],
                              ["img" : "more_wantridicule_icon","title" : "我要吐槽"],
                              ["img" : "icon_more_custom_phone","title" : "客服热线"],
                              ["img" : "icon_more_version","title" : "当前版本"]]
       ]
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tableView.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
       

        self.title = NSLocalizedString("更多", comment: "More")

        self.createTableView(CGRect(x: 0, y: 0, width: SCNConstants.SCREENWIDTH, height: SCNConstants.SCREENHEIGHT - 30), style: .grouped, isRefreshNewData: false, isRefreshMoreData: false) { (tableView) in
            
            tableView.rowHeight = 50
            tableView.register(SCNMoreTableViewCell.classForCoder(), forCellReuseIdentifier: NSStringFromClass(SCNMoreTableViewCell.classForCoder()))
        }

        self.tableView.isHidden = false
        self.tableView.showsVerticalScrollIndicator = false
        self.tableView.snp.makeConstraints { (make) in
            make.top.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview().offset(-30)
        }
    }
    
    func loginAction() {
        // TODO: 登录、注销
        

        if SCNSQLiteManager().isLogin() {

            Alertift.alert(title: "温馨提示", message: "是否退出登录").action(.default("确定"), isPreferred: true){
                
                SCNRequestManager.postRequest([:], URL: SCNRequestAPI.menber().logout, completeBlock: { (result) in
                    NotificationCenter.default.post(name: Notification.Name.Login.LogoutSuccess, object: nil)
                    
                    
                    self.noticeSuccess("成功退出", autoClear: true, autoClearTime: 2)
                    
                    SCNSQLiteManager().delTable()
                    
                    self.tableView.reloadData()
                })
                
                }
                .action(.cancel("取消"))
                .show()

    
        }else {
            let nav = UINavigationController(rootViewController: LoginViewController())
            
            present(nav, animated: true) {}
        }
        
    }
}

extension SCNMoreViewController {
    
    // MARK: - <UITableViewDataSource><UITableViewDelegate>
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return section == 0 ? 1 : 6
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(SCNMoreTableViewCell.classForCoder()), for: indexPath) as! SCNMoreTableViewCell
        
        let sectionObj = dataArray[indexPath.section]
        let rowObj = sectionObj[indexPath.row]
        
        cell.configurationCell(rowObj, indexPath: indexPath)
        
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        defer {
            tableView.deselectRow(at: indexPath, animated: true)
        }
        
        if indexPath.section == 1 {
            
            if indexPath.row == 0 {
                
                let tmpUrl = "\(SCNRequestAPI.H5API)company/index/pagesource/app"
                self.loadWeb(at: self, url: tmpUrl, title: "守财奴简介")
                
            }else if indexPath.row == 1 {
                
                let tmpUrl = "\(SCNRequestAPI.H5API)company/question/pagesource/app"
                self.loadWeb(at: self, url: tmpUrl, title: "常见问题")
                
                
            }else if indexPath.row == 2 {
                
                let tmpUrl = "\(SCNRequestAPI.H5API)company/security/pagesource/app"
                self.loadWeb(at: self, url: tmpUrl, title: "安全保障")
                
            }else if indexPath.row == 3 {
                
                self.scnPushViewController(SCNTsukkomiViewController())
            }else if indexPath.row == 4 {

                UIApplication.shared.open(URL(string: "telprompt://400-861-0186")!, options: [:], completionHandler: { (end) in
                    
                })
            }
        }
        
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return section == 1 ? 50 : 0
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        if section == 1 {
            
            let title = SCNSQLiteManager().isLogin() ? "退出登录":"立即登录"
            
            
            let footView = UIView(frame: CGRect(x: 0, y: 0, width: SCNConstants.SCREENWIDTH, height: 55))
            footView.backgroundColor = SCNConstants.tableViewBackgroundColor
            
            let loginBtn = UIButton(type: .custom)
            
            loginBtn.backgroundColor = SCNConstants.rootBackgroundColor
            loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18.0)
            loginBtn.setTitle(title, for: .normal)
            loginBtn.setTitleColor(UIColor.white, for: .normal)
            loginBtn.layer.cornerRadius = 5.0
            loginBtn.layer.masksToBounds = true
                    loginBtn.addTarget(self, action: #selector(self.loginAction), for: .touchUpInside)
            footView.addSubview(loginBtn)
            
            loginBtn.snp.makeConstraints { (make) in
                make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 40, height: 35))
                make.top.equalTo(20)
                make.centerX.equalTo(footView)
            }
            return footView
        }else {
            return UIView()
        }
        
    }

}
